package com.ruoyi.controller.middlesign.view;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.controller.middlesign.MiddlesMethod;
import com.ruoyi.service.middlesign.MiddlesignService;
import com.ruoyi.util.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zeo
 * @explain 手动推送B2801
 * @date 2021/3/20 15:44
 */
@Controller
@RequestMapping("/middlesignSan")
public class MiddlesignSanController extends BaseController {

    @Autowired
    MiddlesignService middlesignService;

    private String prefix = "middlesign";

    @GetMapping()
    public String middlesignSan(ModelMap mmap) {
        return prefix + "/middlesignsan";
    }

    /**
     * 根据B2803查找在中征平台已授信的供应商
     * <p>
     * * @return
     */
    @RequestMapping("/getCreditList")
    @ResponseBody
    public TableDataInfo getCreditList() {
        List<Map<String, Object>> maps = new ArrayList<>();
        List<Map<String, Object>> hxqyList = middlesignService.findHxqyList();
        for (Map map : hxqyList) {
            String code = map.get("code").toString();

            String name = map.get("name").toString();
            List<Map<String, Object>> maps1 = MiddlesMethod.getCreditList(code, name);
            maps.addAll(maps1);
        }
        return getDataTable(maps);
    }

    /**
     * 接受账款存量数据查询
     *
     * @param map
     * @return
     */
    @RequestMapping("/getStockDataAccounts")
    @ResponseBody
    public TableDataInfo getStockDataAccounts(@RequestParam Map<String, Object> map) {
        String CREDIT_NAME = map.get("CREDIT_NAME").toString();
        String hxqyName = map.get("hxqyName").toString();
        middlesignService.insertAccountsByName(CREDIT_NAME, hxqyName);
        List<Map<String, Object>> maps1 = middlesignService.findAccountsByName(map);
        return getDataTable(maps1);
    }

    /**
     * 接收付款信息存量数据查询
     *
     * @param map
     * @return
     */
    @RequestMapping("/getStockDataPayment")
    @ResponseBody
    public TableDataInfo getStockDataPayment(@RequestParam Map<String, Object> map) {
        String CREDIT_NAME = map.get("CREDIT_NAME").toString();
        String hxqyName = map.get("hxqyName").toString();
        middlesignService.insertPaymentByName(CREDIT_NAME, hxqyName);
        List<Map<String, Object>> maps2 = middlesignService.findPaymentByName(map);
        return getDataTable(maps2);
    }

    /**
     * 接收供应商回款账户数据查询
     *
     * @param map
     * @return
     */
    @RequestMapping("/getStockSupplierAccount")
    @ResponseBody
    public TableDataInfo getStockSupplierAccount(@RequestParam Map<String, Object> map) {
        String CREDIT_NAME = map.get("CREDIT_NAME").toString();
        middlesignService.insertStockSupplierAccount(CREDIT_NAME);
        List<Map<String, Object>> maps3 = middlesignService.findStockSupplierAccount(CREDIT_NAME);
        return getDataTable(maps3);
    }

    /**
     * 接受调账数据
     *
     * @param map
     * @return
     */
    @RequestMapping("/getReconciliationData")
    @ResponseBody
    public TableDataInfo getReconciliationData(@RequestParam Map<String, Object> map) {
        String CREDIT_NAME = map.get("CREDIT_NAME").toString();
        String hxqyName = map.get("hxqyName").toString();
        middlesignService.insertReconciliationData(CREDIT_NAME, hxqyName);
        List<Map<String, Object>> maps4 = middlesignService.findReconciliationData(map);
        return getDataTable(maps4);
    }

    /**
     * 接受账款存量数据推送
     *
     * @param data
     * @return
     */
    @RequestMapping("/accountsPushData")
    @ResponseBody
    public BaseResult accountsPushData(String data, String CREDIT_NAME, String CREDIT_CODE, String hxqyName, String hxqyCode) {
        return middlesignService.accountsPushData(data, CREDIT_CODE, CREDIT_NAME, hxqyCode, "credit");
    }

    /**
     * 接受付款信息数据推送
     *
     * @param data
     * @return
     */
    @RequestMapping("/paymentPushData")
    @ResponseBody
    public BaseResult paymentPushData(String data, String CREDIT_NAME, String CREDIT_CODE, String hxqyName, String hxqyCode) {
        return middlesignService.paymentPushData(data, CREDIT_CODE, CREDIT_NAME, hxqyCode, "credit");
    }

    /**
     * 供应商回款账户数据推送
     *
     * @param data
     * @return
     */
    @RequestMapping("/supplierPush")
    @ResponseBody
    public BaseResult supplierPush(String data, String CREDIT_NAME, String CREDIT_CODE, String hxqyName, String hxqyCode) {
        return middlesignService.supplierPush(data, CREDIT_CODE, hxqyCode, "credit");
    }

    /**
     * 调整数据推送
     *
     * @param data
     * @return
     */
    @RequestMapping("/reconciliationDataPush")
    @ResponseBody
    public BaseResult reconciliationDataPush(String data, String CREDIT_NAME, String CREDIT_CODE, String hxqyName, String hxqyCode) {
        return middlesignService.reconciliationDataPush(data, CREDIT_CODE, CREDIT_NAME, hxqyCode, "credit");
    }


    /**
     * 接受账款修改跳转
     */
    @GetMapping("/editDataAccounts/{id}")
    public String editDataAccounts(@PathVariable("id") String id, ModelMap mmp, String table) {
        mmp.put("stockDataAccounts", middlesignService.getStockDataAccountsById(id, table));
        mmp.put("table", table);
        return prefix + "/editdataaccounts";
    }

    /**
     * 接受账款修改
     *
     * @param map
     * @return
     */
    @PostMapping("/accountsSave")
    @ResponseBody
    public AjaxResult accountsSave(@RequestParam Map<String, Object> map, String table) {
        map.put("table", table);
        return toAjax(middlesignService.accountsSave(map));
    }

    /**
     * 接受付款信息修改跳转
     */
    @GetMapping("/editDataPayment/{id}")
    public String editDataPayment(@PathVariable("id") String id, ModelMap mmp, String table) {
        mmp.put("stockDataPayment", middlesignService.getStockDataPaymentById(id, table));
        mmp.put("table", table);
        return prefix + "/editdatapayment";
    }


    /**
     * 接受付款信息修改
     *
     * @param map
     * @return
     */
    @PostMapping("/paymentSave")
    @ResponseBody
    public AjaxResult paymentSave(@RequestParam Map<String, Object> map, String table) {
        map.put("table", table);
        return toAjax(middlesignService.paymentSave(map));
    }

    /**
     * 修改供应商回款账户数据
     */
    @GetMapping("/editDataSupplierAccount/{id}")
    public String editDataSupplierAccount(@PathVariable("id") String id, ModelMap mmp) {
        mmp.put("supplierAccount", middlesignService.getSupplierAccountById(id));
        return prefix + "/editdatasupplieraccount";
    }

    /**
     * 供应商回款账户数据修改
     *
     * @param map
     * @return
     */
    @PostMapping("/stockSupplierAccountSave")
    @ResponseBody
    public AjaxResult stockSupplierAccountSave(@RequestParam Map<String, Object> map) {
        return toAjax(middlesignService.stockSupplierAccountSave(map));
    }

    /**
     * 修改调账数据
     */
    @GetMapping("/editReconciliation/{id}")
    public String editReconciliation(@PathVariable("id") String id, ModelMap mmp, String table) {
        mmp.put("reconciliation", middlesignService.getReconciliationById(id, table));
        mmp.put("table", table);
        return prefix + "/editdatareconciliation";
    }

    /**
     * 调账数据修改
     *
     * @param map
     * @return
     */
    @PostMapping("/reconciliationSave")
    @ResponseBody
    public AjaxResult reconciliationSave(@RequestParam Map<String, Object> map) {
        return toAjax(middlesignService.reconciliationSave(map));
    }
}
